package com.songcha.module_main.ui.fragment.gongde_rank

import android.os.Bundle
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.runtime.getValue
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.songcha.library_base.mvvm.base.BaseMvvmFragment
import com.songcha.library_business.config.VarConfig
import com.songcha.library_business.helper.AdvertCoverHelper
import com.songcha.library_common.ui.view.compose.ComposeAsyncImage
import com.songcha.library_common.ui.view.compose.ComposeImage
import com.songcha.module_main.R
import com.songcha.module_main.databinding.MainFragGongdeRankBinding
import com.songcha.module_main.event.RefreshGongDeEvent
import com.songcha.module_main.ui.fragment.rank.RankFragment
import com.songcha.module_memo.bean.wooden_fish.QmyRankBean.DataBean.RankBean
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

class GongDeRankFragment:BaseMvvmFragment<GongDeRankViewModel,MainFragGongdeRankBinding>() {

    override fun getLayoutId():Int{
        return R.layout.main_frag_gongde_rank
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun getLoadBackgroundColor(): Int {
        return android.graphics.Color.TRANSPARENT
    }

    override fun onGetData() {
        viewModel.getQmyRankList()
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        databinding.mainCpv.setContent {
            ContentView()
        }

        initAdvert()

        EventBus.getDefault().register(this)
    }

    private fun initAdvert(){
        AdvertCoverHelper.loadAdvertByCoverBanner(requireActivity(),VarConfig.ADVERT_RANK_BANNER_INFO,
            databinding.mainFlAdvertBanner,0f)
    }

    @Composable
    fun ContentView() {
        val rankBean by viewModel.rankBean.collectAsState()
        if(rankBean == null || rankBean!!.rankingList.isEmpty() || rankBean!!.rankingList.size < 3) return

        Column(
            modifier = Modifier.fillMaxSize()
                .verticalScroll(rememberScrollState())
        ) {
             if(rankBean!!.myRanking != null){
                 Box(
                     modifier = Modifier.padding(14.dp,10.dp,14.dp,0.dp)
                         .fillMaxWidth()
                         .border(width = 0.5.dp,color = Color(0xff907664),shape = RoundedCornerShape(5.dp))
                 ){
                     ItemView(0,
                         RankBean(rankBean!!.myRanking.avatar,"我的",rankBean!!.myRanking.todayMeritsVirtues,rankBean!!.myRanking.ranking),
                         rankBean!!.myRanking.ranking,5,5)
                 }

             }

            Row(
                modifier = Modifier.padding(14.dp,23.dp,14.dp)
                    .fillMaxWidth()
            ){
                NoItemView(0,
                    R.mipmap.main_bg_gd_rank_no2,
                    R.mipmap.main_bg_gd_rank_avatar_no2,
                    rankBean!!.rankingList[1].avatar, rankBean!!.rankingList[1].userNick,rankBean!!.rankingList[1].todayMeritsVirtues,
                    0.dp,31.dp)

                NoItemView(15,
                    R.mipmap.main_bg_gd_rank_no1,
                    R.mipmap.main_bg_gd_rank_avatar_no1,
                    rankBean!!.rankingList[0].avatar, rankBean!!.rankingList[0].userNick,rankBean!!.rankingList[0].todayMeritsVirtues,
                    10.dp,0.dp)

                NoItemView(0,
                    R.mipmap.main_bg_gd_rank_no3,
                    R.mipmap.main_bg_gd_rank_avatar_no3,
                    rankBean!!.rankingList[2].avatar, rankBean!!.rankingList[2].userNick,rankBean!!.rankingList[2].todayMeritsVirtues,
                    10.dp,47.dp)

            }

            if(rankBean!!.rankingList.size < 4) return

            val list = rankBean!!.rankingList.subList(3,rankBean!!.rankingList.size)

            Box(
                modifier = Modifier.padding(14.dp,33.dp,14.dp,10.dp)
                    .fillMaxWidth()
                    .border(width = 0.5.dp,color = Color(0xff907664),shape = RoundedCornerShape(5.dp))
                    .background(Color(0xff22201F))
            ){
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                   /* itemsIndexed(list){index,item->
                        ItemView(index)
                    }*/
                    for(i in list.indices){
                        ItemView(i,list[i])
                    }
                }
            }


        }
    }

    @Composable
    private fun ItemView(index:Int, item: RankBean,rank:String? = null,marginTop:Int = 0, marginBottom:Int = 19){
        var mt = marginTop
        if(marginTop == 0)
            mt = if(index == 0) 15 else 0
        Row(
            modifier = Modifier.padding(top = mt.dp, bottom = marginBottom.dp)
                .fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ){
            var rankStr = ""
            if(rank != null){
                rankStr = rank
            }else{
                rankStr = String.format("%02d",index + 4)
            }

            Text(
                modifier = Modifier.padding(start = 8.dp),
                text = rankStr, fontSize = 16.sp,color = Color(0xffFFCCA9),
                fontWeight = FontWeight.Bold)

            Row(
                modifier = Modifier.padding(14.dp,0.dp,10.dp)
                    .weight(1f)
                    .height(36.dp)
                    .clip(RoundedCornerShape(18.dp,2.dp,2.dp,18.dp))
                    .background(Color(0xff1A1A1A)),
                verticalAlignment = Alignment.CenterVertically
            ){
                Box(
                    modifier = Modifier.size(36.dp)
                        .clip(RoundedCornerShape(18.dp))
                        .background(Color(0xffFFCCA9))
                ){
                    ComposeAsyncImage(
                        modifier = Modifier.padding(start = 1.dp,top = 1.dp)
                            .size(34.dp)
                            .clip(RoundedCornerShape(17.dp)),
                        url = item.avatar)
                }

                Text(
                    modifier = Modifier.padding(start = 10.dp)
                        .weight(1f),
                    text = item.userNick, fontSize = 14.sp,color = Color(0xffDCAB89), fontWeight = FontWeight.Bold,
                    maxLines = 1, overflow = TextOverflow.Ellipsis)

                Text(
                    modifier = Modifier.padding(),
                    text = "功德：${item.todayMeritsVirtues}", fontSize = 12.sp,color = Color(0xffDCAB89))

                ComposeImage(
                    modifier = Modifier.padding(start = 9.dp,end = 9.dp)
                        .size(22.dp),
                    id = R.mipmap.main_icon_gd_rank_gd)


            }
        }
    }

    @Subscribe
    fun onRefreshGongDe(event:RefreshGongDeEvent){
        val frag = (requireParentFragment() as RankFragment)
        if(!frag.isCurrentBottomTab()) return
        onGetData()
    }

    @Composable
    private fun NoItemView(heightDiff:Int,bgResId:Int,avatarResId:Int,
                           avatar:String,
                           nickName:String,
                           gongde:Int,
                           marginLeft: Dp,marginTop:Dp){
        Box(
            modifier = Modifier.padding(start = marginLeft,top = marginTop)
                .size(104.dp,(158 + heightDiff).dp)
        ){
            ComposeImage(
                modifier = Modifier.fillMaxSize(),
                id = bgResId)

            Column (
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally
            ){
                //main_bg_gd_rank_avatar_no1
                Box(
                    modifier = Modifier.padding(top = (27 + heightDiff).dp)
                        .size(63.dp,70.dp)

                ){
                    ComposeImage(
                        modifier = Modifier.fillMaxSize(),
                        id = avatarResId)

                    Box(
                        modifier = Modifier.padding(start = 12.dp,top = 18.dp)
                            .size(45.dp)
                            .clip(RoundedCornerShape(22.5.dp))
                            //.background(Color.Red)
                    ){
                        ComposeAsyncImage(
                            modifier = Modifier.fillMaxSize(),
                            url = avatar)
                    }

                }

                Text(
                    modifier = Modifier.padding(top = 8.dp),
                    text = nickName,
                    fontSize = 14.sp,color = Color(0xffCFCDCB),
                    maxLines = 1, overflow = TextOverflow.Ellipsis,
                    fontWeight = FontWeight.Bold)

                Text(
                    modifier = Modifier.padding(top = 5.dp),
                    text = "功德:${gongde}",
                    fontSize = 11.sp,color = Color(0xffCFCDCB))

            }

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}